package com.angel.application.commandgateways;


import com.angel.application.commandgateways.commands.PublishVoiceCommand;
import com.angel.domain.angelvoice.aggregates.AngelVoice;
import com.angel.domain.angelvoice.event.PublishAngelVoiceEvent;
import com.angel.domain.angelvoice.factory.AngelVoiceFactory;
import com.angel.domain.angelvoice.repositories.AngelVoiceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/*
 * 更新关注微信公众号： 为了offer
 * @author parkin
 * @description 声音（评论）
 * @date 2021/11/18
 */
@Service
public class AngelVoiceCommandService {
    @Autowired
    private ApplicationContext applicationContext;

    private final AngelVoiceRepository angelVoiceRepository;

    public AngelVoiceCommandService(AngelVoiceRepository angelVoiceRepository) {
        this.angelVoiceRepository = angelVoiceRepository;
    }

    public void publishVoice(PublishVoiceCommand publishVoiceCommand) {

        AngelVoice angelVoice = AngelVoiceFactory.create(
                publishVoiceCommand.getHelpId(),
                publishVoiceCommand.getPublishUserId(),
                publishVoiceCommand.getContent()
        );

        // todo 事件暂时要依赖spring， 实现jdk的很麻烦， 后续使用mq
        PublishAngelVoiceEvent publishAngelVoiceEvent = angelVoice.publishVoice();

        angelVoiceRepository.save(angelVoice);
        applicationContext.publishEvent(publishAngelVoiceEvent);

    }
}
